'use strict';

import {ajax, nameSpace } from 'utils/index';
import jsonp from 'jsonp';

let ns = nameSpace('Zhekou1AllInList');

export const GET_LIST_DONE = ns('GET_LIST_DONE');
export const GET_DETAIL_DONE = ns('GET_DETAIL_DONE');
export const EDIT_ACTIVE = ns('EDIT_ACTIVE');
export const REMOVE_ACTIVE = ns('REMOVE_ACTIVE');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const EDIT_DIALOG = ns('EDIT_DIALOG');
export const SET_CUR_PARAM = ns('SET_CUR_PARAM');
export const LOAD_START = ns('LOAD_START');
export const LOAD_ERR = ns('LOAD_ERR');

export function closeDialog() {
  return {type: CLOSE_DIALOG,visible:false};
}
export function setActiveParam(param) {
  return {type: SET_CUR_PARAM,param};
}
export function showEditDialog(active) {
  return {type: EDIT_DIALOG,show:true,active};
}
export function closeEditDialog() {
  return {type: EDIT_DIALOG,show:false,active:false};
}
export function removeActive(activity_id) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		if(window.workbench){
			//千牛环境执行
			  window.QN.top.invoke( {
					cmd : 'taobao.promotionmisc.item.activity.delete',
					param : {activity_id},
					method:"post",
					success : function(rsp, cmd, param) {
						dispatch({type: REMOVE_ACTIVE,activity_id});
					},
					error : function(msg, cmd, param) {
						dispatch({
							type: LOAD_ERR,
							message:json.msg+(json.sub_msg?json.sub_msg:""),
							loginFalse:false
						});
					}
				} );
		}else{
			//web环境执行创建折扣
			ajax({
				  api: 'removeShopActive',
				  method: 'GET',
				  data:{activity_id}
				},(json) => {
				  //成功后
					dispatch({type: REMOVE_ACTIVE,activity_id});
				},(json) => {
				  //失败后
				  //(json.code==53) W2超时
				  const login = (json.code==210);
				  dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
					});
			});
		}
	}
}
export function editActive(new_active) {
	if(new_active.start_time instanceof Date){
		const ts = new_active.start_time;
		new_active.start_time = (ts.getFullYear()+"-"+(ts.getMonth()+1)+"-"+ts.getDate()+" "+ts.getHours()+":"+ts.getMinutes()+":"+ts.getSeconds() );
	}
	if(new_active.end_time instanceof Date){
		const ts = new_active.end_time;
		new_active.end_time = (ts.getFullYear()+"-"+(ts.getMonth()+1)+"-"+ts.getDate()+" "+ts.getHours()+":"+ts.getMinutes()+":"+ts.getSeconds() );
	}
	const params = {
					activity_id : new_active.activity_id,
					name : new_active.name,
					start_time : new_active.start_time,
					end_time : new_active.end_time,
					is_discount : new_active.is_discount,
					discount_rate : new_active.discount_rate*100,
					is_decrease_money : new_active.is_decrease_money,
					decrease_amount : new_active.decrease_amount*100
				}
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START,
		  data: params
		});
		if(window.workbench){
			//千牛环境执行
			  window.QN.top.invoke( {
					cmd : 'taobao.promotionmisc.item.activity.update',
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			//web环境执行创建折扣
			ajax({
				  api: 'editShopActive',
				  method: 'GET',
				  data:params
				},(json) => {
				  //成功后
					dispatch({
					  type: EDIT_ACTIVE,
					  data: new_active
					});
				},(json) => {
				  //失败后
				  //(json.code==53) W2超时
				  const login = (json.code==210);
				  dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
					});
			});
		}
	}
}
export function getList(params) {
    return (dispatch) => {
    //发送请求前
    dispatch({
      type: LOAD_START,
      data: params
    });
	if(window.workbench){
		//千牛环境执行
		  window.QN.top.invoke( {
				cmd : 'taobao.promotionmisc.item.activity.list.get',
				param : params,
				method:"post",
				success : function(rsp, cmd, param) {
					console.log(rsp,param);
				},
				error : function(msg, cmd, param) {
					console.log(msg,param);
				}
			} );
	}else{
		//web环境执行创建折扣
		ajax({
			  api: 'getShopActiveList',
			  method: 'GET',
			  data:params
			},(json) => {
			  //成功后
				if(json.item_promotion_list && !(json.item_promotion_list.item_promotion instanceof Array)){
					json.item_promotion_list.item_promotion = Array(json.item_promotion_list.item_promotion);
				}
			    dispatch({
				  type: GET_LIST_DONE,
				  data: json
				});

			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}

  };
}

export function getActiveDetail(params) {
    return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START,
		  data: params
		});

		if(window.workbench){
			//千牛环境执行
			  window.QN.top.invoke( {
					cmd : 'taobao.promotionmisc.item.activity.get',
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			//web环境执行创建折扣
			ajax({
				  api: 'getShopActiveDetail',
				  method: 'GET',
				  data:params
				},(json) => {
					const curActive = json.item_promotion;
					curActive.decrease_amount = curActive.decrease_amount/100;
					curActive.discount_rate = curActive.discount_rate/100;
					dispatch({
						type: GET_DETAIL_DONE,
						data:json,
						curActive
					});
				},(json) => {
				  //失败后
				  //(json.code==53) W2超时
				  const login = (json.code==210);
				  dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
					});
			});
		}
	}
}